/**
 * 找到数组中重复个数大于数组数组一半长度的数字
 */

function moreThanHalfNumber(arr) {
    let result, count = 0;
    for (let i = 0; i < arr.length; i++) {
        if (count === 0) {
            result = arr[i]
            count++
        } else {
            if (result === arr[i]) {
                count++
            } else {
                count--
            }
        }
    }
    const times = arr.filter(item => item === result).length
    return times > Math.floor(arr.length >> 1) ? result : 0
}

const arr = [1, 1, 1, 1, 2, 3, 4]
console.log(moreThanHalfNumber(arr));
